#include "RadioBox.h"
#include "../../ImageManager.h"

namespace PickinOpenSticks {
    namespace Ui {

        void RadioBox::Draw(sf::RenderTarget *target) {
            if (!checked) target->Draw(sprite);
            else target->Draw(uncheckedSprite);
        }

        void RadioBox::SetCheckedImage(const std::string& filename) {
            sprite.SetImage(ImageManager::GetSingleton().GetImage(filename, filename));
            pos.w = (int) sprite.GetSize().x;
            pos.h = (int) sprite.GetSize().y;
        }

        void RadioBox::SetUnCheckedImage(const std::string& filename) {
            uncheckedSprite.SetImage(ImageManager::GetSingleton().GetImage(filename, filename));
        }

        void RadioBox::SetDefaultImage() {
            sprite.SetImage(ImageManager::GetSingleton().GetImage("assets/gfx/checkbox1.png", "default_checked"));
            uncheckedSprite.SetImage(ImageManager::GetSingleton().GetImage("assets/gfx/radiobox2.png", "default_unchecked_radio"));

            pos.w = (int) sprite.GetSize().x;
            pos.h = (int) sprite.GetSize().y;
        }

        void RadioBox::Check() {
            //if it's a main widget.. interact with the other main radio boxes
            if (!parent) {
                vector<Widget*> rbox = Manager::GetSingleton().GetWidgetsByType(RADIOBOX);
                for (int i = 0; i < (int) rbox.size(); i++) {
                    ((RadioBox*) rbox[i])->UnCheck();
                }
                checked = true;
            } else {
                const WidgetList& Ui = parent->GetWidgetList();
                for (WidgetList::const_iterator i = Ui.begin(); i != Ui.end(); i++) {
                    if (i->second->GetType() == RADIOBOX) {
                        ((RadioBox*) i->second)->UnCheck();
                    }
                }
                checked = true;
            }
        }
    }
}
